﻿using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Repository.Interface.TraceabilityInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.MaHaiBo.Service.TraceabilityService
{
    public class PlotInfoService : IPlotInfoService
    {
        /// <summary>
        ///日志
        /// </summary>
        ILogger<PlotInfoService> logger;
        /// <summary>
        /// 溯源仓储接口
        /// </summary>
        ITracingRepository TracingR;
        /// <summary>
        /// 商品仓储接口
        /// </summary>
        ICommodityRepository CommodityR;
        /// <summary>
        /// 图片仓储接口
        /// </summary>
        IPlotInfoRepository PlotInfoR;
        /// <summary>
        /// 企业
        /// </summary>
        IFirmRepository FirmR;
        /// <summary>
        /// 屠宰
        /// </summary>
        ISlaughterRepository SlaughterR;
        /// <summary>
        /// 深加工
        /// </summary>
        IDeepProcessingRepository DeepProcessingR;
        /// <summary>
        /// 包材
        /// </summary>
        IPackingMaterialsRepository PackingMaterialsR;
        /// <summary>
        /// 添加剂
        /// </summary>
        IAdditiveRepository AdditiveR;
        /// <summary>
        /// 辅材
        /// </summary>
        IAccessoriesRepository AccessoriesR;
        /// <summary>
        /// 原材
        /// </summary>
        IRawMaterialRepository RawMaterialR;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="TracingR">溯源仓储接口</param>
        /// <param name="CommodityR">商品仓储接口</param>
        /// <param name="PlotInfoR">图片仓储接口</param>
        /// <param name="FirmR">企业</param>
        /// <param name="SlaughterR">屠宰</param>
        /// <param name="DeepProcessingR">添加剂</param>
        /// <param name="PackingMaterialsR">深加工</param>
        /// <param name="PackingMaterialsR">深加工</param>
        /// <param name="AdditiveR">添加剂</param>
        /// <param name="AccessoriesR">辅材</param>
        /// <param name="RawMaterialR">原材</param>
        public PlotInfoService(ILogger<PlotInfoService> logger,
        ITracingRepository TracingR,
        ICommodityRepository CommodityR,
        IPlotInfoRepository PlotInfoR,
        IFirmRepository FirmR,
        ISlaughterRepository SlaughterR,
         IDeepProcessingRepository DeepProcessingR,
          IPackingMaterialsRepository PackingMaterialsR,
           IAdditiveRepository AdditiveR,
           IAccessoriesRepository AccessoriesR,
          IRawMaterialRepository RawMaterialR)
        {
            this.logger = logger;
            this.TracingR = TracingR;
            this.CommodityR = CommodityR;
            this.PlotInfoR = PlotInfoR;
            this.FirmR = FirmR;
            this.SlaughterR = SlaughterR;
            this.DeepProcessingR = DeepProcessingR;
            this.PackingMaterialsR = PackingMaterialsR;
            this.AdditiveR = AdditiveR;
            this.AccessoriesR = AccessoriesR;
            this.RawMaterialR = RawMaterialR;
        }

        /// <summary>
        /// 添加产品检验报告
        /// </summary>
        /// <param name="tracingId">溯源编号</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddsurfacePlot(int tracingId, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.TracingId = tracingId;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);

            }
            catch (Exception ex)
            {
                logger.LogError("【图片服务层】添加产品检验报告 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 产品检验报告
        /// </summary>
        /// <param name="tracingId">溯源编号</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据列表</returns>
        public async Task<List<PlotInfo>> Tracinglist(int tracingId, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.TracingId == tracingId && p.LOT == lot);
            }
            catch (Exception ex)
            {
                logger.LogError("【图片服务层】产品检验报告 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加动态证
        /// </summary>
        /// <param name="Hygieneid">养殖编号</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddHygienePlot(int farmid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.farmid = farmid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);

            }
            catch (Exception ex)
            {
                logger.LogError("【图片服务层】添加产品检验报告 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 动态证
        /// </summary>
        /// <param name="farmid">养殖编号</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据列表</returns>
        public async Task<List<PlotInfo>> HygienePlotInfolist(int farmid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.farmid == farmid && p.LOT == lot);

            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】动态证 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加企业
        /// </summary>
        /// <param name="firmid">企业编号</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> AddFirmPlot(int firmid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.firmid = firmid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);

            }
            catch (Exception ex)
            {
                logger.LogError("【图片服务层】添加企业 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 企业图片
        /// </summary>
        /// <param name="firmid">企业编号</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据列表</returns>
        public async Task<List<PlotInfo>> FirmPlotInfolist(int firmid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.firmid == firmid && p.LOT == lot);

            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】企业图片 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加相关资料
        /// </summary>
        /// <param name="slaughterid">屠宰id</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> AddSlaughterPlotInfolist(int slaughterid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.slaughterid = slaughterid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】添加相关资料 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 相关资料图片
        /// </summary>
        /// <param name="slaughterid">屠宰id</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据集合</returns>
        public async Task<List<PlotInfo>> SlaughterPlotInfolist(int slaughterid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.slaughterid == slaughterid && p.LOT == lot);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】相关资料图片 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加深加工相关资料
        /// </summary>
        /// <param name="Deepprocessingid">深加工id</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> AddDeepProcessingPlotInfolist(int Deepprocessingid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.DeepProcessingid = Deepprocessingid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】添加深加工相关资料 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 深加工相关资料图片
        /// </summary>
        /// <param name="Deepprocessingid">深加工id</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据集合</returns>
        public async Task<List<PlotInfo>> DeepProcessingPlotInfolist(int Deepprocessingid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.DeepProcessingid == Deepprocessingid && p.LOT == lot);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】深加工相关资料图片 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加原材相关资料
        /// </summary>
        /// <param name="RawMaterialid">原材id</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> AddRawMaterialPlotInfolist(int RawMaterialid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.RawMaterialid = RawMaterialid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】添加原材相关资料 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 原材相关资料图片
        /// </summary>
        /// <param name="RawMaterialid">原材id</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据集合</returns>
        public async Task<List<PlotInfo>> RawMaterialPlotInfolist(int RawMaterialid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.RawMaterialid == RawMaterialid && p.LOT == lot);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】原材相关资料图片 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加辅料相关资料
        /// </summary>
        /// <param name="Accessoriesid">辅料id</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> AddAccessoriesPlotInfolist(int Accessoriesid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.Accessoriesid = Accessoriesid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】添加辅料相关资料 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 辅料相关资料图片
        /// </summary>
        /// <param name="Accessoriesid">辅料id</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据集合</returns>
        public async Task<List<PlotInfo>> AccessoriesPlotInfolist(int Accessoriesid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.Accessoriesid == Accessoriesid && p.LOT == lot);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】辅料相关资料图片 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加添加剂相关资料
        /// </summary>
        /// <param name="Additiveid">添加剂id</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> AddAdditivePlotInfolist(int Additiveid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.Additiveid = Additiveid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】添加添加剂相关资料 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加剂相关资料图片
        /// </summary>
        /// <param name="Additiveid">添加剂id</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据集合</returns>
        public async Task<List<PlotInfo>> AdditivePlotInfolist(int Additiveid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.Additiveid == Additiveid && p.LOT == lot);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】添加剂相关资料图片 出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 包材添加相关资料
        /// </summary>
        /// <param name="PackingMaterialsid">包材id</param>
        /// <param name="lot">批次</param>
        /// <param name="surfacePlot">图片</param>
        /// <returns></returns>
        public async Task<int> PackingMaterialsPlotInfolist(int PackingMaterialsid, string? lot, string? surfacePlot)
        {
            try
            {
                var Plotin = new PlotInfo();

                Plotin.PackingMaterialsid = PackingMaterialsid;
                Plotin.LOT = lot;
                Plotin.Plot = surfacePlot;

                return await PlotInfoR.AddAsync(Plotin);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】包材添加相关资料 出错" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 包才相关资料图片
        /// </summary>
        /// <param name="PackingMaterialsid">包才id</param>
        /// <param name="lot">批次</param>
        /// <returns>返回数据集合</returns>

        public async Task<List<PlotInfo>> PackingMaterialsPlotInfolist(int PackingMaterialsid, string? lot)
        {
            try
            {
                return await PlotInfoR.GetAsync(p => p.PackingMaterialsid == PackingMaterialsid && p.LOT == lot);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】包才相关资料图片 出错" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="Pid">图片编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DelPlotInfo(int Pid)
        {
            try
            {
                var list = await PlotInfoR.GetModelAsync(Pid);
                return await PlotInfoR.DeleteAsync(list);
            }
            catch (Exception ex)
            {

                logger.LogError("【图片服务层】删除图片 出错" + ex.Message);
                throw;
            }
        }

    }
}
